import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'// 数据持久化插件 防止页面刷新丢失数据
import { removeToken, setToken } from '@/common/auth'
import { xorjiami } from '@/common/requestEncy'
import { JSEncrypt } from 'jsencrypt'
const getEncCode = (str) => { // 注册方法
  const pubKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvCkyEkJSy7QWRPZzceERKiMEtdPfCvJbEJj93IqE4nX4CAwuNESEx5jVTBG7TfvOCbUTdcRh9MHPQ0XldVPs/HO8v95OMUXh1+bDrDBPPh3tSgh+2YZB/WxJAY6V6kNPXXnsZBHCpQqTt0INbPrjTGcVNKFphDSmuom3/TxHd5c0dH33xGSL8U0/KXTWto3QmhdAOqCErhMdn6MMNjyaU5qnHq9x0B5shEHSyp3hkBRCcnScnCRRVqTNbeJ2iiyfX6Dd8RqVA9emlj9jHetU+ot7DwwhdMDS2vT6xb32HHEYemN85bPXpjkawuHznKks0dmc5ceHxAalNpmneIVvMwIDAQAB' // ES6 模板字符串 引用 rsa 公钥
  const encryptStr = new JSEncrypt()
  encryptStr.setPublicKey(pubKey) // 设置 加密公钥
  const data = encryptStr.encrypt(str.toString()) // 进行加密
  return data
}
import CryptoJS from '@/common/aes'
CryptoJS.$getAES = (encrypted) => {
  var key = CryptoJS.enc.Hex.parse('e2f29cb8df39a368e2f29cb8df39a368')
  var iv = CryptoJS.enc.Hex.parse('5e92442523d44e7c167fcc6f98d40157')
  var string = encrypted
  var data = CryptoJS.AES.decrypt(string, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  var uncrypted = data.toString(CryptoJS.enc.Utf8)
  return uncrypted
}

Vue.use(Vuex)
const store = new Vuex.Store({
  plugins: [createPersistedState({
    storage: window.sessionStorage
  })],
  state: {
    userName: '',
    userInfo: '', // 用户信息
    isLogin: false, // 是否需要登录
    browserHeaderTitle: '',
    // axios取消请求
    axiosPromiseCancel: [],
    keepAliveArr: [],
    unionList: []// 统一登录平台手机号对应的多系统列表
  },
  getters: {
    loginState: state => state.isLogin,
    userInfo: state => state.userInfo,
    indexHeaderBg: state => state.indexHeaderBg,
    browserHeaderTitle: state => state.browserHeaderTitle,
    axiosPromiseCancel: state => state.axiosPromiseCancel,
    keepAliveArr: state => state.keepAliveArr,
    unionList: state => state.unionList
  },
  mutations: {
    SET_LOGINSTATE: (state, boolen) => {
      state.isLogin = boolen
    },
    SET_USERINFO: (state, data) => {
      state.userInfo = data
    },
    SET_BROWSERHEADERTITLE: (state, action) => {
      state.browserHeaderTitle = action.browserHeaderTitle
    },
    ADD_AXIOS_CANCEL: (state, component) => {
      !state.axiosPromiseCancel.includes(component) && state.axiosPromiseCancel.push(component)
    },
    REMOVE_AXIOS_CANCEL: (state, index) => {
      state.axiosPromiseCancel.length > index && state.axiosPromiseCancel.splice(index, 1)
    },
    CLEAR_AXIOS_CANCEL: (state) => {
      state.axiosPromiseCancel = []
    },
    ADD_KEEP_ALIVE: (state, component) => {
      !state.keepAliveArr.includes(component) && state.keepAliveArr.push(component)
    },
    REMOVE_KEEP_ALIVE: (state, component) => {
      const index = state.keepAliveArr.indexOf(component)
      index > -1 && state.keepAliveArr.splice(index, 1)
    },
    CLEAR_KEEP_ALIVE: (state) => {
      state.keepAliveArr = ['Index']
    },
    SET_UNIONLIST: (state, array) => {
      state.unionList = array
    }
  },
  actions: {
    setNoticeBar({ commit }, height) {
      commit('SET_NOTICEBAR', height)
    },
    setLoginState({ commit }, boolen) {
      commit('SET_LOGINSTATE', boolen)
    },
    setUserInfo({ commit }, data) {
      commit('SET_USERINFO', data)
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        removeToken()
        resolve()
      })
    },
    // 对指定组件进行动态更改缓存（缓存）--组件调用该方法时，判断该组件是否存在于该缓存数组，无则添加
    addKeepAliveArr({ commit }, component) {
      commit('ADD_KEEP_ALIVE', component)
    },
    // 对指定组件进行动态更改缓存（不缓存）--组件调用该方法时，从缓存数组中删除对应的组件元素
    removeKeepAliveArr({ commit }, component) {
      commit('REMOVE_KEEP_ALIVE', component)
    },
    // 对指定组件进行动态更改缓存（不缓存）--组件调用该方法时，从缓存数组中删除对应的组件元素
    clearKeepAliveArr({ commit }) {
      commit('CLEAR_KEEP_ALIVE')
    },
    setUnionList({ commit }, array) {
      commit('SET_UNIONLIST', array)
    }
  }
})
export default store
